package com.dldata.devtools30.controller.zhibiao;

import com.alibaba.fastjson.JSON;
import com.dldata.devtools30.entity.*;
import com.dldata.devtools30.entity.DevtZbplanInfoEntity;
import com.dldata.devtools30.repository.zhibiao.DevtZbobjInfoEntityDao;
import com.dldata.devtools30.repository.zhibiao.DevtZbplanInfoEntityDao;
import com.dldata.devtools30.repository.zhibiao.DevtZbplanParaEntityDao;
import com.dldata.devtools30.repository.zhibiao.DevtZbplanTypeEntityDao;
import com.dldata.devtools30.utils.Result;
import com.dldata.devtools30.utils.StringUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "/DevtZbplanInfoEntityController")
@Slf4j
public class DevtZbplanInfoEntityController {

    @Autowired
    DevtZbplanInfoEntityDao devtZbplanInfoEntityDao;
    
    @Autowired
    DevtZbplanTypeEntityDao devtZbplanTypeEntityDao;
    
    @Autowired
    DevtZbplanParaEntityDao devtZbplanParaEntityDao;
    
    @RequestMapping(value = "/getTree")
    @ResponseBody
    public Result getTree(@RequestParam Map<String, String> paramMap){
        String serach=paramMap.get("serach");
        serach=StringUtil.isEmptyOrLength0(serach)?"":serach;
        serach="%"+serach+"%";
    
        List<DevtZbplanInfoEntity> zbplanInfoEntityArrayList =Lists.newArrayList(devtZbplanInfoEntityDao.getAll(serach));
        Map<String, List<DevtZbplanInfoEntity>> zbplanInfoEntityGroupMap
                = zbplanInfoEntityArrayList.stream().collect(Collectors.groupingBy(DevtZbplanInfoEntity::getTypeid));
        
        List<HashMap> hashMapList=new ArrayList<>();
        List<DevtZbplanTypeEntity> typeEntityArrayList =devtZbplanTypeEntityDao.getAll(serach,zbplanInfoEntityGroupMap.keySet());
        typeEntityArrayList.sort(new Comparator<DevtZbplanTypeEntity>() {
            @Override
            public int compare(DevtZbplanTypeEntity o1, DevtZbplanTypeEntity o2) {
                if(o1.getXh()==null) o1.setXh(0);
                if(o2.getXh()==null) o2.setXh(0);
                return o1.getXh().compareTo(o2.getXh());
            }
        });
        
        for (int i = 0; i < typeEntityArrayList.size(); i++) {
            DevtZbplanTypeEntity devtZbplanTypeEntity = typeEntityArrayList.get(i);
            HashMap hashMap=new HashMap();
            hashMap.put("id",devtZbplanTypeEntity.getId());
            hashMap.put("title",devtZbplanTypeEntity.getTypename());
            hashMap.put("children",new ArrayList<>());
            hashMap.put("spread",true);
            hashMapList.add(hashMap);
    
            List<DevtZbplanInfoEntity> infoEntityList
                    = zbplanInfoEntityGroupMap.getOrDefault(devtZbplanTypeEntity.getId(), new ArrayList<>());
            for (int j = 0; j < infoEntityList.size(); j++) {
                DevtZbplanInfoEntity devtZbplanInfoEntity = infoEntityList.get(j);
                HashMap hashMapSub=new HashMap();
                hashMapSub.put("id",devtZbplanInfoEntity.getId());
                hashMapSub.put("title",devtZbplanInfoEntity.getPlanname());
                ((List)hashMap.get("children")).add(hashMapSub);
            }
            
        }
        
        return Result.ok("",hashMapList);
    }
    
    @RequestMapping(value = "/getAll")
    @ResponseBody
    public Result getAll(@RequestParam Map<String, String> paramMap){
        
        List<DevtZbplanInfoEntity> zbplanInfoEntityArrayList =Lists.newArrayList(devtZbplanInfoEntityDao.findAll());
    
        zbplanInfoEntityArrayList.sort(new Comparator<DevtZbplanInfoEntity>() {
            @Override
            public int compare(DevtZbplanInfoEntity o1, DevtZbplanInfoEntity o2) {
                if(o1.getXh()==null) o1.setXh(0);
                if(o2.getXh()==null) o2.setXh(0);
                return o1.getXh().compareTo(o2.getXh());
            }
        });
        
        return Result.ok(zbplanInfoEntityArrayList);
    }
    
    
    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(DevtZbplanInfoEntity entity){
        if(StringUtil.isEmptyOrLength0(entity.getPlanname())){
            return Result.error("名称不能为空");
        }
        
        if(StringUtil.isEmptyOrLength0(entity.getId())){
            entity.setId(UUID.randomUUID().toString());
        }
        entity.setTypecode(devtZbplanTypeEntityDao.findById(entity.getTypeid()).orElse(new DevtZbplanTypeEntity()).getTypecode());
        devtZbplanInfoEntityDao.save(entity);
        
        return Result.ok();
    }
    
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(@RequestParam Map<String, String> paramMap){
        
        String id =paramMap.get("id");
        if(StringUtil.isEmptyOrLength0(id)){
            return  Result.error("参数有误");
        }
        try {
            DevtZbplanInfoEntity devtZbclassInfoEntity = devtZbplanInfoEntityDao.findById(id).orElse(null);
            if(devtZbclassInfoEntity!=null){
                devtZbplanInfoEntityDao.delete(devtZbclassInfoEntity);
            }
        }
        catch (Exception ex){
            log.error("【执行"+this.getClass().getName()+".delete() 方法失败 】异常："+ex.getMessage(),ex);
            return  Result.error("操作错误，请稍候再试!");
        }
        return  Result.ok("删除成功");
    }
    
    @RequestMapping("edit")
    public String toEdit(String id, Model model){
        DevtZbplanInfoEntity zbobjInfoEntity = new DevtZbplanInfoEntity();
        if(!StringUtil.isEmptyOrLength0(id)){
            zbobjInfoEntity = devtZbplanInfoEntityDao.findById(id).orElse(new DevtZbplanInfoEntity());
        }
        model.addAttribute("entity",zbobjInfoEntity);
        
        List<DevtZbplanTypeEntity> zbclassInfoEntityList = Lists.newArrayList(devtZbplanTypeEntityDao.findAll());
        model.addAttribute("typeList",zbclassInfoEntityList);
        
        return "thymeleaf/devtools30/zhibiao/v_zhibiaoplanInfoEdit";
    }
    
    @RequestMapping("toList")
    public String toList(Model model){
        
        return "thymeleaf/devtools30/zhibiao/v_zhibiaoplanInfoList";
    }
    
    @RequestMapping("toTestSql")
    public String toTestSql(Model model,String planid){
        model.addAttribute("planid",planid);
    
        List<DevtZbplanParaEntity> paraEntityList = devtZbplanParaEntityDao.getAllByPlanid(planid);
        
        model.addAttribute("paraList", JSON.toJSONString(paraEntityList));
        
        return "thymeleaf/devtools30/zhibiao/v_zhibiaoplansqltest";
    }
}
